﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Globalization;

namespace ICDA.Web.Models
{
    public class ActivityViewModelBinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            ActivityViewModel activitymodel = (ActivityViewModel)base.BindModel(controllerContext, bindingContext);
            if (controllerContext.HttpContext.Request.Params["ID"] != "_empty")
            {
                activitymodel.ID = Convert.ToInt32(controllerContext.HttpContext.Request.Params["ID"]);
            }
            if (controllerContext.HttpContext.Request.Params["ID"] != "_empty")
            {
                activitymodel.UserID = controllerContext.HttpContext.Request.Params["UserID"];
            }
            activitymodel.Task = controllerContext.HttpContext.Request.Params["Task"];
            activitymodel.TimeSpend = Convert.ToInt32(controllerContext.HttpContext.Request.Params["TimeSpend"]);
            if (controllerContext.HttpContext.Request.Params["OverTime"] == "")
            {
                activitymodel.OverTime = 0;
            }
            else
            {
                activitymodel.OverTime = Convert.ToInt32(controllerContext.HttpContext.Request.Params["OverTime"]);
            }
            if (controllerContext.HttpContext.Request.Params["oper"] != "del")
            {
                List<string> dateBreak = controllerContext.HttpContext.Request.Params["ActivityDate"].Trim().Split('-').ToList();
                string actDate = String.Format("{0}/{1}/{2}", dateBreak[0], dateBreak[1], dateBreak[2]);
                activitymodel.ActivityDate = DateTime.ParseExact(actDate, "d", System.Globalization.CultureInfo.InvariantCulture);
            }
            return activitymodel;
        }
    }
}
